package com.guigu.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.community.mapper.InventoryMapper;
import com.guigu.community.mapper.ProductImageMapper;
import com.guigu.community.mapper.ProductMapper;
import com.guigu.community.pojo.Inventory;
import com.guigu.community.pojo.ProductCategory;
import com.guigu.community.pojo.ProductImage;
import com.guigu.community.pojo.Products;
import com.guigu.community.service.ProductService;
import com.guigu.community.vo.InventoryVo;
import com.guigu.community.vo.ProductRequestVo;
import com.guigu.community.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Products> implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private ProductImageMapper productImageMapper;

    @Override
    public PageInfo<Products> queryVo(Integer pageNo, Integer pageSize, ProductVo vo) {
        PageHelper.startPage(pageNo, pageSize);
        List<Products> products = productMapper.selectVo(vo);
        PageInfo<Products> pageInfo = new PageInfo<>(products);
        return pageInfo;
    }

    @Override
    public Products queryById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<ProductCategory> queryByLevel() {
        return productMapper.selectByLevel();
    }

    @Override
    public List<Products> queryProductType(Integer suppliersId) {
        return productMapper.selectProductType(suppliersId);
    }

    @Override
    public boolean addProduct(ProductRequestVo request) {
        // 插入商品信息
        Products product = new Products();
        product.setName(request.getName());
        product.setDescription(request.getDescription());
        product.setSalePrice(request.getSalePrice().floatValue());
        product.setStatus((byte) 1);
        product.setCreatedAt(new Date());
        product.setUpdatedAt(new Date());
        int productRows = productMapper.insert(product);
        if (productRows <= 0) {
            throw new RuntimeException("商品信息插入失败");
        }

        // 插入库存信息
        Inventory inventory = new Inventory();
        inventory.setProductId(product.getProductId());
        inventory.setWarehouseId(request.getWarehouseId());
        inventory.setQuantity(request.getInitialStock());
        inventory.setCreatedAt(new Date());
        inventory.setUpdatedAt(new Date());
        int inventoryRows = inventoryMapper.insert(inventory);

        // 插入商品图片信息
        List<String> imageUrls = request.getImageUrls();
        for (int i = 0; i < imageUrls.size(); i++) {
            ProductImage productImage = new ProductImage();
            productImage.setProductId(product.getProductId());
            productImage.setImageUrl(imageUrls.get(i));
            productImage.setCreatedAt(new Date());
            // 标记第一张图片为主图
            productImage.setImageType(i == 0 ? 1 : 2);
            int imageRows = productImageMapper.insert(productImage);
            if (imageRows <= 0) {
                throw new RuntimeException("商品图片信息插入失败");
            }
        }

        return inventoryRows > 0;
    }

    @Override
    public boolean updateById(Products product) {
        int result = productMapper.updateById(product);
        return result > 0;
    }

    @Override
    public PageInfo<Products> queryProductType(Integer pageNo, Integer pageSize, ProductVo vo) {
        PageHelper.startPage(pageNo, pageSize);
        List<Products> products = productMapper.selectProducts(vo);
        PageInfo<Products> pageInfo = new PageInfo<>(products);
        return pageInfo;
    }

    @Override
    public List<Products> queryId() {
        return productMapper.listId();
    }
}